package com.balisong.y;

import com.alibaba.fastjson.JSON;
import com.balisong.y.pojo.Good;
import com.balisong.y.pojo.info.GoodInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author balisong_fcs
 * @date 2020/12/19 20:38
 */
@SpringBootTest
public class testController {
    private RestHighLevelClient client;

    @Before
    public void init() {
        client = new RestHighLevelClient(RestClient.builder(new HttpHost("192.168.200.150", 9200, "http")));
    }




    @Test
    public void testC() throws IOException {
        SearchSourceBuilder builder = new SearchSourceBuilder();
        /*bool*/
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        /*should*/
        boolQueryBuilder.should(QueryBuilders.queryStringQuery("华为").defaultField("name").analyzer("ik_max_word"));
        builder.query(boolQueryBuilder);
        /*设置结果高亮*/
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.field("name").preTags("<font style='color:red'>").postTags("</font>");
        builder.highlighter(highlightBuilder);
        /*分页*/
        builder.from(1).size(10);
        SearchRequest request = new SearchRequest("sku");
        request.source(builder);
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        long totalNum = response.getHits().getTotalHits().value;
        List<GoodInfo> list = new ArrayList<>();
        for (SearchHit hit : response.getHits().getHits()) {
            String goodJsonStr = hit.getSourceAsString();
            Good good = JSON.parseObject(goodJsonStr, Good.class);
            HighlightField highlightField = hit.getHighlightFields().get("name");
            Text[] highlightFieldFragments = highlightField.getFragments();
            String highname = StringUtils.join(highlightFieldFragments);
            if (StringUtils.isNotBlank(highname)) {
                good.setName(highname);
            }
            GoodInfo goodInfo = new GoodInfo();
            BeanUtils.copyProperties(good, goodInfo);
            list.add(goodInfo);
            System.out.println(goodInfo);
        }
    }
}
